package com.fss.daoTestCases;

import java.util.*;

import com.fss.secondHand.dao.*;

import com.fss.experience.dao.*;

import junit.framework.TestCase;
import com.fssDataModels.*;

import com.fss.activity.*;
import com.fss.activity.dao.ActivityDaoImpl;
import com.fss.user.dao.*;
import com.fss.common.dao.*;

public class TestCommonDaoImpl extends TestCase{
	public TestCommonDaoImpl(
	        String name) {
	        super(name);
	    }
	
	private UserDaoImpl db = new UserDaoImpl();//connection to database
	private ActivityDaoImpl db2 =  new ActivityDaoImpl();
	private CommonDaoImpl dbCommon = new CommonDaoImpl();
	private ExperienceDaoImpl dbEx = new ExperienceDaoImpl();
	private SecondHandDaoImpl dbSecondHand = new SecondHandDaoImpl();
	
	
	
	/*=================get activities===========================*/
	    public void testGetAdminActivities() {//numOfResult>0, userType=0
	    	TbUser author = db.getUser("fss_admin1");
	    	TbUser normalUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        
	     
	        
	        db.insertUser(normalUser);
	    	
	    	TbActivity newActivity0 = new TbActivity(author,"dummy activity0 ","dummy content",new Date());
	    	TbActivity newActivity1 = new TbActivity(author,"dummy activity1 ","dummy content",new Date());
	    	TbActivity newActivity2 = new TbActivity(author,"dummy activity2 ","dummy content",new Date());
	    	TbActivity newActivity3 = new TbActivity(normalUser,"dummy activity3 ","dummy content",new Date());
	        
	    	db2.insertActivity(newActivity0);
	    	db2.insertActivity(newActivity1);
	    	db2.insertActivity(newActivity2);
	    	db2.insertActivity(newActivity3);
	    	
	    	List adminActivities = dbCommon.getActivities(10,0);
	    	
	    	assertEquals(3,adminActivities.size() );
	    	
	    	db2.deleteActivity(newActivity0);
	    	db2.deleteActivity(newActivity1);
	    	db2.deleteActivity(newActivity2);
	    	db2.deleteActivity(newActivity3);
	    	db.deleteUser(normalUser);
	    	
	    }
	    
	    public void testGetUserActivites() {//numOfResult>0, userType!=0
	    	TbUser author = db.getUser("fss_admin1");
	    	TbUser normalUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        
	     
	        
	        db.insertUser(normalUser);
	    	
	    	TbActivity newActivity0 = new TbActivity(author,"dummy activity0 ","dummy content",new Date());
	    	TbActivity newActivity1 = new TbActivity(author,"dummy activity1 ","dummy content",new Date());
	    	TbActivity newActivity2 = new TbActivity(author,"dummy activity2 ","dummy content",new Date());
	    	TbActivity newActivity3 = new TbActivity(normalUser,"dummy activity3 ","dummy content",new Date());
	        
	    	db2.insertActivity(newActivity0);
	    	db2.insertActivity(newActivity1);
	    	db2.insertActivity(newActivity2);
	    	db2.insertActivity(newActivity3);
	    	
	    	List adminActivities = dbCommon.getActivities(10,1);
	    	
	    	assertEquals(1,adminActivities.size() );
	    	
	    	db2.deleteActivity(newActivity0);
	    	db2.deleteActivity(newActivity1);
	    	db2.deleteActivity(newActivity2);
	    	db2.deleteActivity(newActivity3);
	    	db.deleteUser(normalUser);
	    }
	    
	    public void testGetZeroActivites() {//numOfResult=0
	    	TbUser author = db.getUser("fss_admin1");
	    	TbUser normalUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        
	     
	        
	        db.insertUser(normalUser);
	    	
	    	TbActivity newActivity0 = new TbActivity(author,"dummy activity0 ","dummy content",new Date());
	    	TbActivity newActivity1 = new TbActivity(author,"dummy activity1 ","dummy content",new Date());
	    	TbActivity newActivity2 = new TbActivity(author,"dummy activity2 ","dummy content",new Date());
	    	TbActivity newActivity3 = new TbActivity(normalUser,"dummy activity3 ","dummy content",new Date());
	        
	    	db2.insertActivity(newActivity0);
	    	db2.insertActivity(newActivity1);
	    	db2.insertActivity(newActivity2);
	    	db2.insertActivity(newActivity3);
	    	
	    	List adminActivities = dbCommon.getActivities(0,1);
	    	
	    	assertEquals(null,adminActivities);
	    	
	    	db2.deleteActivity(newActivity0);
	    	db2.deleteActivity(newActivity1);
	    	db2.deleteActivity(newActivity2);
	    	db2.deleteActivity(newActivity3);
	    	db.deleteUser(normalUser);
	    }
	    
	    
	    /*=================get experiences===========================*/
	    public void testGetExperiences_FromDateNull() {//numOfResult=null
	    	TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");
	        
	        dbEx.insertExperienceType(type);
	        
	        TbExperience newExperience0 = new TbExperience(type,author,"dummy experience",new Date("05/21/2011"),"no content",0);
	        TbExperience newExperience1 = new TbExperience(type,author,"dummy experience",new Date("05/21/2009"),"no content",0);
	        TbExperience newExperience2 = new TbExperience(type,author,"dummy experience",new Date("05/21/2009"),"no content",0);
	        TbExperience newExperience3 = new TbExperience(type,author,"dummy experience",new Date("05/21/2009"),"no content",0);
	        
	        
	        dbEx.insertExperience(newExperience0);
	        dbEx.insertExperience(newExperience1);
	        dbEx.insertExperience(newExperience2);
	        dbEx.insertExperience(newExperience3);
	        
	        List experiences = dbCommon.getExperiences(type, null, new Date());
	        
	        TbExperience ex= (TbExperience)experiences.get(0);
	        	        	        	        
	        assertEquals(3,experiences.size());
	        
	        dbEx.deleteExperience(newExperience0);
	        dbEx.deleteExperience(newExperience1);
	        dbEx.deleteExperience(newExperience2);
	        dbEx.deleteExperience(newExperience3);
	        
	        dbEx.deleteExperienceType(type.getTypeId());
	        
	        
	        
	        
	    }
	    public void testGetExperiences_TypeNull() 
	    {
	    	TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");	        
	        dbEx.insertExperienceType(type);
	        
	        TbExperienceType type2 = new TbExperienceType();
	        type.setTypeName("type2");
	        dbEx.insertExperienceType(type2);
	        
	        TbExperience newExperience0 = new TbExperience(type,author,"dummy experience",new Date("05/21/2009"),"no content",0);
	        TbExperience newExperience1 = new TbExperience(type,author,"dummy experience",new Date("05/21/2009"),"no content",0);
	        TbExperience newExperience2 = new TbExperience(type2,author,"dummy experience",new Date("05/23/2009"),"no content",0);
	        TbExperience newExperience3 = new TbExperience(type2,author,"dummy experience",new Date("05/25/2009"),"no content",0);
	        
	        
	        dbEx.insertExperience(newExperience0);
	        dbEx.insertExperience(newExperience1);
	        dbEx.insertExperience(newExperience2);
	        dbEx.insertExperience(newExperience3);
	        
	        List experiences = dbCommon.getExperiences(null, new Date("05/22/2009"), new Date());
	        
	        
	        	        	        	        
	        assertEquals(2,experiences.size());
	        
	        dbEx.deleteExperience(newExperience0);
	        dbEx.deleteExperience(newExperience1);
	        dbEx.deleteExperience(newExperience2);
	        dbEx.deleteExperience(newExperience3);
	        
	        dbEx.deleteExperienceType(type.getTypeId());
	        dbEx.deleteExperienceType(type2.getTypeId());
	        
	        
	    	
	    }
	    
	    
	    public void testGetExperiences() {//numOfResult=null
	    	TbUser author = db.getUser("fss_admin1");
	        TbExperienceType type = new TbExperienceType();
	        type.setTypeName("type1");
	        
	        dbEx.insertExperienceType(type);
	        
	        TbExperience newExperience0 = new TbExperience(type,author,"dummy experience",new Date("05/21/2011"),"no content",0);
	        TbExperience newExperience1 = new TbExperience(type,author,"dummy experience",new Date("05/21/2009"),"no content",0);
	        TbExperience newExperience2 = new TbExperience(type,author,"dummy experience",new Date("05/21/2000"),"no content",0);
	        TbExperience newExperience3 = new TbExperience(type,author,"dummy experience",new Date("05/21/2009"),"no content",0);
	        
	        
	        dbEx.insertExperience(newExperience0);
	        dbEx.insertExperience(newExperience1);
	        dbEx.insertExperience(newExperience2);
	        dbEx.insertExperience(newExperience3);
	        
	        List experiences = dbCommon.getExperiences(type, new Date("05/21/2000"), new Date("05/21/2000"));
	        
	        TbExperience ex= (TbExperience)experiences.get(0);
	        	        	        	        
	        assertEquals(1,experiences.size());
	        
	        dbEx.deleteExperience(newExperience0);
	        dbEx.deleteExperience(newExperience1);
	        dbEx.deleteExperience(newExperience2);
	        dbEx.deleteExperience(newExperience3);
	        
	        dbEx.deleteExperienceType(type.getTypeId());	        	        	        	        
	    }
	    
	    
	    /*=================get secondhand items===========================*/
	    public void testGetSecondHands_FromDateNull() {//numOfResult=null
	    	TbUser newUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
		       
	        db.insertUser(newUser);
	    	
	    	TbUser author = db.getUser("user2");
	    	
	    	TbSecondhandItem newItem0 = new TbSecondhandItem(author,"special second hand 0","dummy content",new Date("05/21/2010"));
	    	TbSecondhandItem newItem1 = new TbSecondhandItem(author,"special second hand 1","dummy content",new Date("05/21/2010"));
	    	TbSecondhandItem newItem2 = new TbSecondhandItem(author,"special second hand 2","dummy content",new Date("05/21/2010"));
	    	TbSecondhandItem newItem3 = new TbSecondhandItem(author,"special second hand 3","dummy content",new Date("05/21/2010"));
	    	
	    	
	    	dbSecondHand.insertSecondHandItem(newItem0);
	    	dbSecondHand.insertSecondHandItem(newItem1);
	    	dbSecondHand.insertSecondHandItem(newItem2);
	    	dbSecondHand.insertSecondHandItem(newItem3);
	    	
	    	List items = dbCommon.getSecondHandItems(null, new Date());
	    	
	    	assertEquals(4,items.size());
	    	
	    	dbSecondHand.deleteSecondHandItem(newItem0);
	    	dbSecondHand.deleteSecondHandItem(newItem1);
	    	dbSecondHand.deleteSecondHandItem(newItem2);
	    	dbSecondHand.deleteSecondHandItem(newItem3);
	    	
	    	db.deleteUser(newUser);
	    	
	    	
	    	
	    }
	    
	    public void testGetSecondHands() {//numOfResult=null
	    	TbUser newUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
		       
	        db.insertUser(newUser);
	    	
	    	TbUser author = db.getUser("user2");
	    	
	    	TbSecondhandItem newItem0 = new TbSecondhandItem(author,"special second hand 0","dummy content",new Date("05/21/2010"));
	    	TbSecondhandItem newItem1 = new TbSecondhandItem(author,"special second hand 1","dummy content",new Date("06/21/2010"));
	    	TbSecondhandItem newItem2 = new TbSecondhandItem(author,"special second hand 2","dummy content",new Date("07/21/2010"));
	    	TbSecondhandItem newItem3 = new TbSecondhandItem(author,"special second hand 3","dummy content",new Date("08/21/2010"));
	    	
	    	
	    	dbSecondHand.insertSecondHandItem(newItem0);
	    	dbSecondHand.insertSecondHandItem(newItem1);
	    	dbSecondHand.insertSecondHandItem(newItem2);
	    	dbSecondHand.insertSecondHandItem(newItem3);
	    	
	    	List items = dbCommon.getSecondHandItems(new Date("07/21/2010"), new Date());
	    	
	    	assertEquals(2,items.size());
	    	
	    	dbSecondHand.deleteSecondHandItem(newItem0);
	    	dbSecondHand.deleteSecondHandItem(newItem1);
	    	dbSecondHand.deleteSecondHandItem(newItem2);
	    	dbSecondHand.deleteSecondHandItem(newItem3);
	    	
	    	db.deleteUser(newUser);
	    	
	    
	    }
	    
	    /*=================get users===========================*/
	    public void testGetUsers_FromDateNull() {//numOfResult=null
	    	TbUser newUser0 = new TbUser("user0","ansinh","123456","ansinh@ansinh.com","as","as", new Date("07/21/2010"),"2010-12-13");
	    	TbUser newUser1 = new TbUser("user1","ansinh","123456","ansinh@ansinh.com","as","as", new Date("07/21/2010"),"2010-12-13");
	    	TbUser newUser2 = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date("07/21/2010"),"2010-12-13");
	    	TbUser newUser3 = new TbUser("user3","ansinh","123456","ansinh@ansinh.com","as","as", new Date("07/21/2010"),"2010-12-13");
	    	
	    	
	        db.insertUser(newUser0);
	        db.insertUser(newUser1);
	        db.insertUser(newUser2);
	        db.insertUser(newUser3);
	        
	        List users = dbCommon.getUsers(null, new Date());
	        
	        assertEquals(7,users.size());// 3 admins + 4 newly created users
	        
	        db.deleteUser(newUser0);
	        db.deleteUser(newUser1);
	        db.deleteUser(newUser2);
	        db.deleteUser(newUser3);	        	       
	    }
	    
	    public void testGetUsers() {//numOfResult=null
	    	TbUser newUser0 = new TbUser("user0","ansinh","123456","ansinh@ansinh.com","as","as", new Date("12/15/2010"),"2010-12-13");
	    	TbUser newUser1 = new TbUser("user1","ansinh","123456","ansinh@ansinh.com","as","as", new Date("12/17/2010"),"2010-12-13");
	    	TbUser newUser2 = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date("02/21/2010"),"2010-12-13");
	    	TbUser newUser3 = new TbUser("user3","ansinh","123456","ansinh@ansinh.com","as","as", new Date("07/21/2010"),"2010-12-13");
	    	
	    	
	        db.insertUser(newUser0);
	        db.insertUser(newUser1);
	        db.insertUser(newUser2);
	        db.insertUser(newUser3);
	        
	        List users = dbCommon.getUsers(new Date("12/15/2010"), new Date());
	        
	        assertEquals(2,users.size());
	        
	        db.deleteUser(newUser0);
	        db.deleteUser(newUser1);
	        db.deleteUser(newUser2);
	        db.deleteUser(newUser3);
	    }
	    /*=================CMS contents===========================*/
	    public void testGetCmsContents() {//numOfResult=null
	        List cmsContents = dbCommon.getCmsContents();
	    	
	        assertEquals(3, cmsContents.size());
	    }
	    
	    public void testUpdateCmsContents() {//numOfResult=null
	    	List cmsContents = dbCommon.getCmsContents();
	    	
	    	TbCmsContent cms = (TbCmsContent)cmsContents.get(0);
	    	cms.setContent("content 0 updated");
	    	
	    	cms = (TbCmsContent)cmsContents.get(1);
	    	cms.setContent("content 1 updated");
	    	
	    	cms = (TbCmsContent)cmsContents.get(2);
	    	cms.setContent("content 2 updated");
	    	
	    	
	    	dbCommon.updateCmsContents(cmsContents);
	    	
	    	cmsContents = dbCommon.getCmsContents();
	    	
	    	cms = (TbCmsContent)cmsContents.get(0);	    	
	        assertEquals("content 0 updated", cms.getContent());
	        
	        cms = (TbCmsContent)cmsContents.get(1);	    	
	        assertEquals("content 1 updated", cms.getContent());
	        
	        cms = (TbCmsContent)cmsContents.get(2);	    	
	        assertEquals("content 2 updated", cms.getContent());
	    }
	    
	    
	    /*=================entry point of the testing program===========================*/
	    public static void main(String[] args) {
	        junit.textui.TestRunner.run(
	        		TestCommonDaoImpl.class);
	    }
}
